package seguridad;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author kastillo
 */
public class ConvertirArchivo {

  public static byte[] fileToByte(String ruta)  
  {
        byte[] bytes = null;
        FileInputStream fis = null;
        try {
            File file = new File(ruta);
            fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            try {
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum); //no doubt here is 0
                    //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                    System.out.println("read " + readNum + " bytes,");
                }
            } catch (IOException ex) {
            }
            bytes = bos.toByteArray();
            return bytes;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConvertirArchivo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(ConvertirArchivo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return bytes;
    }
   public static void byteToFile(String ruta, byte[] bytes) 
   {
        FileOutputStream fos = null;
        try {
            //below is the different part
            File someFile = new File(ruta);
            fos = new FileOutputStream(someFile);
            fos.write(bytes);
            fos.flush();
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(ConvertirArchivo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(ConvertirArchivo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

   public static void borrarArchivo(String rutaCompleta) {
          File file = new File(rutaCompleta);
           file.delete();              
    }

   public static String quitarExtension(String rutaCompleta){

        String sinExtension = rutaCompleta.substring(0,rutaCompleta.lastIndexOf("."));
        return sinExtension;
            
     }
  public static String traerExtension(String rutaCompleta){

        String sinExtension = rutaCompleta.substring(rutaCompleta.lastIndexOf(".")+1);
        return sinExtension;
 }
    public static void main(String[] args) {
        System.out.println(""+ConvertirArchivo.traerExtension("alskdfjald.jxta"));
    }


}
